package com.qingxin.sys.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.SysRole;
import com.qingxin.sys.entity.SysUser;
import com.qingxin.sys.entity.SysUserRoleRel;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.utils.StringUtils;

/**
 * 用户角色
 * @author Administrator
 *
 */
@Service
public class SysUserRoleRelService extends BaseService<SysUserRoleRel, String> {
	
	@Autowired
	private SysPermissionService sysPermissionService;
	@Autowired
	private SysUserService sysUserService;

	
	/**
	 * 获取指定用户的角色列表
	 * @param userid
	 * @param loginUser
	 * @param pageParam
	 * @param request
	 * @return
	 */
	public Pager listUserRoles(String userid, LoginUser loginUser, PageParam pageParam, HttpServletRequest request){
		Querier querier = Querier.create("from SysUserRoleView where userid=:userid order by role_name asc");
		querier.setParam("userid", userid);
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	
	/**
	 * 获取没有分配给指定用户的所有角色
	 * @param userid
	 * @param loginUser
	 * @param pageParam
	 * @param request
	 * @return
	 */
	public Pager listUserExRoles(String userid, LoginUser loginUser, PageParam pageParam, HttpServletRequest request){
		SysUser user = sysUserService.get(userid);
		if(user==null || StringUtils.isEmpty(user.getGroupid())){
			return new Pager();
		}
		String groupid = user.getGroupid();
		
		Querier querier = Querier.create("select new com.qingxin.sys.entity.SysRole(r.roleid, r.role_name, r.create_time, g.groupname)" +
				" from SysRole r, SysGroup g where r.groupid=g.groupid ");
		
		if(groupid.length() > 4){
			querier.append("and ( r.groupid like :groupid");
			querier.setParam("groupid", groupid + "%");
			
			querier.append(" or ");
			
			List<String> idList = new ArrayList<String>();
			String sgid = groupid;
			while(sgid.length() > 4){
				sgid = sgid.substring(0, sgid.length()-4);
				idList.add(sgid);
			}
			String idset = getSqlInParam(idList);
			
			querier.append(" (r.subgroup=1 and r.groupid in " + idset + ") ");
			
			querier.append(" or g.grouptype=9 ");
			
			querier.append(")");
		}else {
			querier.append("and ( r.groupid like :groupid");
			querier.setParam("groupid", groupid + "%");
			querier.append(" or g.grouptype=9 )");
		}
		
		querier.append(" and r.roleid not in (select rel.sysRole.roleid from SysUserRoleRel rel where rel.sysUser.userid=:userid)");
		querier.setParam("userid", userid);
		querier.append(" order by r.role_name asc");
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	
	/**
	 * 保存用户角色
	 * @param userid
	 * @param roleids
	 * @param loginuser
	 * @param request
	 * @return
	 */
	public Object saveUserRoleRels(String userid, String[] roleids, LoginUser loginuser, HttpServletRequest request){
		if(StringUtils.isEmpty(userid) || roleids == null){
			return null;
		}
		sysPermissionService.evictUserRightsCache(userid);

		Date now = new Date();
		for(String roleid : roleids){
			if(StringUtils.isEmpty(roleid)){
				continue;
			}
			if(isUserRoleRelExisted(userid, roleid)){
				continue;
			}
			SysUserRoleRel rel = new SysUserRoleRel(new SysUser(userid), new SysRole(roleid), loginuser.getUsername(), now);
			this.save(rel);
		}
		return null;
	}
	
	
	/**
	 * 判断指定关系是否存在
	 * @param userid
	 * @param roleid
	 * @return
	 */
	public boolean isUserRoleRelExisted(String userid, String roleid){
		String hql = "select relid from SysUserRoleRel where sysUser.userid=? and sysRole.roleid=? ";
		Object rel = this.getByHql(hql, userid, roleid);
		return rel!=null;
	}
	
	
	/**
	 * 删除用户角色关联关系
	 * @param relids
	 * @param request
	 * @return
	 */
	public Object deleteUserRoleRels(String[] relids, HttpServletRequest request){
		for(String relid : relids){
			SysUserRoleRel rel = get(relid);
			if(rel == null){
				continue;
			}
			this.delete(rel);
			
			sysPermissionService.evictUserRightsCache(rel.getSysUser().getUserid());
		}
		
		return null;
	}
	
	
	
}
